package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/17 19:44
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result add(Map map) {
        //1、验证用户的预约时间是否有预约设置
        String orderDate = (String) map.get("orderDate");
        Date date = null;
        try {
            date = DateUtils.parseString2Date(orderDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用orderSettingDao通过日期查询是否有设定
        OrderSetting orderSetting=orderSettingDao.getByDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2、用户预定的套餐是否还有剩余
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //3、用户是否是会员
        //获取手机号通过手机号来查询用户是否是会员
        String telephone = (String) map.get("telephone");
        Member member=memberDao.getByTelephone(telephone);
        Integer memberId = null;
        if (member!=null) {
            memberId = member.getId();
        }else{
            //添加会员并获取会员id
            Member member1 = new Member();
            String sex=(String) map.get("sex");
            member1.setSex(sex);
            String name=(String) map.get("name");
            member1.setName(name);
            String idCard = (String) map.get("idCard");
            member1.setIdCard(idCard);
            member1.setPhoneNumber(telephone);
            //添加注册时间
        Date date1 = new Date();
        member1.setRegTime(date1);
        memberDao.add(member1);
        memberId=member1.getId();
    }
    //4、用户所选的套餐是否存在
    String setmealIdString = (String) map.get("setmealId");
    Integer setmealId = Integer.parseInt(setmealIdString);
    Setmeal setmeal = setmealDao.getById(setmealId);
        if (setmeal == null) {
        return new Result(false, "当前套餐不存在");
    }

    //5、用户是否在同一天同一套餐重复预定,根据会员id，日期，套餐进行判断
        Order order = new Order();
        order.setMemberId(memberId);
        order.setSetmealId(setmealId);
        order.setOrderDate(date);
        List<Order> orderList = orderDao.getByCondition(order);
        if (orderList.size() > 0) {
        return new Result(false, MessageConstant.HAS_ORDERED);
    }

    //6、进行订单添加，并返回id
    Order addOrder = new Order();
        addOrder.setMemberId(memberId);
        addOrder.setOrderDate(date);
        addOrder.setOrderType("微信预约");
        addOrder.setSetmealId(setmealId);
        addOrder.setOrderStatus("未到诊");
    //进行添加预约返回订单id
        orderDao.add(addOrder);
    //更改orderSetting中的数据,Reservation预定数量+1
        orderSettingDao.editReservation(date);
        Integer id = addOrder.getId();
        System.out.println(id);
        return new Result(true,MessageConstant.ORDER_SUCCESS,addOrder.getId());
}

    @Override
    public Map getById(Integer id) {

        return orderDao.getById4Detail(id);
    }
}
